home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / ms-0_06.lha / bms-0.06 / ms_real.h < prev    next >
C/C++ Source or Header  |  1993-08-06  |  4KB  |  118 lines

  1. /* ms_real.h - define real and complex number formats and macros */
  2. /* Copyright (C) 1990, 1991 Andreas Gustafsson */
  3.  
  4. /* Three potentially different abstract data types are used to
  5. represent real numbers: "double", "real", and "netreal".  "double" is
  6. what the client program uses internally and is supposed to be easy to
  7. use from C and accurate but not necessarily fast nor portable (as in
  8. having a standardized binary representation that is compatible between
  9. machines).  "real" is what the computation server uses internally.
  10. Additions, multiplications, and magnitude comparisions of "real"
  11. values are supposed to be fast; generality and portability are
  12. secondary.  Finally, the "netreal" type is the format in which the
  13. client and the server communicate real numbers over the network.
  14. "netreal" numbers must be in a well-defined, portable format that is
  15. easily converted to and from the other formats.
  16.  
  17. The "netreal" format in the current protocol version is a 32-bit two's
  18. complement fixed-point number with a 7 bit integer part and 25 bit
  19. fractional part, transmitted in network byte order.  The "double" type
  20. is the native C "double" on the client machine.  The "real" type is
  21. currently the same as "netreal" on 680x0, Vax, 80386, and MIPS
  22. processors, and a native C "double" on all other machines. */
  23.  
  24. #ifndef _ms_real_h
  25. #define _ms_real_h
  26.  
  27. /* Sony defines mc68020 but not mc68000; fix it */
  28. #ifdef mc68020
  29. #ifndef mc68000
  30. #define mc68000
  31. #endif
  32. #endif
  33.  
  34. /* Currently fixed-point arithmetic is supported only with GCC on */
  35. /* 680x0, Vax, i386, and MIPS processors */
  36. #ifdef __GNUC__
  37. #ifdef mc68000
  38. #define REAL_FIXED
  39. #endif
  40. #ifdef vax
  41. #define REAL_FIXED
  42. #endif
  43. #ifdef i386
  44. #define REAL_FIXED
  45. #endif
  46. #ifdef mips
  47. /* Fixed point is supported for MIPS processors, but at least on a */
  48. /* DECStation it is slower than floating point */
  49. /* #define REAL_FIXED */ 
  50. #endif
  51. #endif
  52.  
  53. /* All others machines use doubles */
  54. #ifndef REAL_FIXED
  55. #define REAL_DOUBLE    /* use floating-point arithmetic */
  56. #endif
  57.  
  58. /* definitions for fixed-point numbers */
  59. typedef long fixed;
  60. #define ALLBITS 32
  61. #define LEFTBITS 7
  62. #define RIGHTBITS (ALLBITS-LEFTBITS)
  63. #define one_fixed() (1<<RIGHTBITS)
  64.  
  65. /* conversion between fixed-point numbers and doubles */
  66. #define double_to_fixed(x) ((real)((x) * ((double)one_fixed())))
  67. #define fixed_to_double(x) ((double)(x) * (1/(double)one_fixed()))
  68.  
  69. /* how to implement the necessary real number primitives using doubles */
  70. #ifdef REAL_DOUBLE
  71. typedef double real;
  72. #define add_real(x,y) ((x)+(y))
  73. #define sub_real(x,y) ((x)-(y))
  74. #define mul_real(x,y) ((x)*(y))
  75. #define mul_real_int(x,i) ((x)*(double)(i))
  76. #define gteq_real(x,y) ((x)>=(y))
  77. #define twice_mul_real(x,y) ((x)*(y)*2.0)
  78. #define zero_real() (0.0)
  79. #define four_real() (4.0)
  80. #define int_to_real(x) ((double)(x))
  81. #define double_to_real(x) (x)
  82. #define fixed_to_real(x) fixed_to_double(x)
  83. #endif
  84.  
  85. /* how to implement the necessary real number primitives using */
  86. /* fixed-point numbers */
  87. #ifdef REAL_FIXED
  88. typedef fixed real;
  89. #define add_real(x,y) ((x)+(y))
  90. #define sub_real(x,y) ((x)-(y))
  91. #define mul_real(x,y) fracmult(x,y)
  92. #define mul_real_int(x,i) ((x)*(long)(i))
  93. #define gteq_real(x,y) ((x)>=(y))
  94. #define twice_mul_real(x,y) fracmult2(x,y)
  95. #define zero_real() (0L)
  96. #define four_real() (one_fixed()*4L)
  97. #define int_to_real(x) ((x) << RIGHTBITS)
  98. #define double_to_real(x) double_to_fixed(x)
  99. #define fixed_to_real(x) (x)
  100. #endif
  101.  
  102. /* these defines reflect the fact that the "net" type is currently "fixed" */
  103. #define netreal fixed
  104. #define double_to_net(x) double_to_fixed(x)
  105. #define net_to_real(x) fixed_to_real(x)
  106.  
  107. typedef struct
  108. { real re; 
  109.   real im;
  110. } complex;
  111.  
  112. typedef struct
  113. { netreal re; 
  114.   netreal im;
  115. } netcomplex;
  116.  
  117. #endif /* _ms_real_h */
  118.